package com.liy.jiagou.resources;


import com.liy.jiagou.common.api.entity.ResultVo;
import com.liy.jiagou.entity.UserEntity;
import com.liy.jiagou.feign.ProductFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 用户 Controller
 */
@RestController
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     *  随便一个接口服, 如果用户没有  登录认证的信息 -> token
     *  那么是无权进行访问的
     *
     */

    @GetMapping("/index")
    ResultVo index() {
        return ResultVo.ok("Welcome to Sys Server ....","Access Sys Server is OK");
    }

    /**
     *  随便一个接口服, 如果用户没有  登录认证的信息 -> token
     *  那么是无权进行访问的
     *
     */
    @GetMapping("/")
    ResultVo home() {
        return ResultVo.ok("Welcome to Sys Server ....","Access Sys Server is OK");
    }



    @PreAuthorize("hasAuthority('sys.query')")
    @GetMapping("/query/user")
    UserEntity getUser(@RequestParam("username") String  username) {

        logger.info("------------------- UserController-------------- getProduct() "    );

        return new UserEntity(username,"user123456");
    }



    @PreAuthorize("hasAuthority('sys.add')")
    @PostMapping("/add/user")
    UserEntity addUser(UserEntity user) {
        logger.info("------------------- UserController-------------- addUser() "    );
        return user;
    }


    @Autowired
    private ProductFeignClient productFeignClient;

    /**
    *   需求  查看用户的所有购买的 product
    *
    **/
    @GetMapping("/user/query/products")
    ResultVo getUserBuyProducts() {

        logger.info("------------------- sys-server 调用   product-server 服务 ------------- " );

        // 调用 product 服务
        ResultVo resultVo = productFeignClient.getProduct("feign-1001");

        return resultVo;
    }

}
